package com.spider.controller;

import com.spider.service.QuartzServiceI;
import com.spider.task.PrintTimeTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tqc")
public class TestQuartzController extends BaseController{

    @Autowired
    private QuartzServiceI qsi;


    @RequestMapping("/testQuartz")
    public  String testQuartz(){
        return prefix + "/testQuartz" + suffix;
    }
    @RequestMapping("/editCron")
    public  String editCron(){
        return prefix + "/editCron" + suffix;
    }



    @RequestMapping("/addQuartz")
    @ResponseBody
    public Map<String,Object> addQuartz(@PathParam(value = "cron") String cron){
        Map<String,Object> map = new HashMap<>();
        try {
            qsi.init(cron,0,
                    "job1","jobGroup",
                    "tri1","triGroup",
                    PrintTimeTask.class.getName());
            map.put("msg","启动成功");
            map.put("is",true);
        }catch (Exception e){
            map.put("msg","启动失败");
            map.put("is",false);
        }
        return map;
    }


    @RequestMapping("/pauseQuartz")
    @ResponseBody
    public Map<String,Object> pauseQuartz(){
        Map<String,Object> map = new HashMap<>();
        boolean b = qsi.pauseJob("job1","jobGroup");
        map.put("msg","任务暂停"+(b?"成功":"失败"));
        return map;
    }

    @RequestMapping("/resumeQuartz")
    @ResponseBody
    public Map<String,Object> resumeQuartz(){
        Map<String,Object> map = new HashMap<>();
        boolean b = qsi.resumeJob("job1","jobGroup");
        map.put("msg","任务启动"+(b?"成功":"失败"));
        return map;
    }

    @RequestMapping("/recent5TriggerTime")
    @ResponseBody
    public List<String> recent5TriggerTime(@PathParam(value="cron") String cron){
        List<String> list = qsi.recent5TriggerTime(cron,5);
        return list;
    }

}
